package 工厂方法;

import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

public class ClientGUI extends Application {
	public static void main(String[] args) {
		launch(args);
		}
		public void start(Stage primaryStage)throws Exception {
	        primaryStage.setTitle("工厂方法模式");
	        Button btn = new Button();
	        btn.setText("显示保险信息");
	        
	        Label lab1=new Label("姓名：   詹沛霖    ");
	        Label lab2=new Label("学号：   201627010228 ");
	        Label lab3=new Label("请选择保险类型：  ");
	        ChoiceBox cb1=new ChoiceBox(FXCollections.observableArrayList("bodyinjur","cardam","personinjure"));
	     
	    	
	    	TextField txt=new TextField();
	    	txt.setMinWidth(250);
	    	txt.setMinHeight(200);
	   
	    	HBox h1=new HBox();
	   	 
	   	 GridPane gri=new GridPane();
	   	 gri.setPadding(new Insets(5));
	   	 gri.setHgap(5);
	   	 gri.setVgap(5);
	     
	        gri.add(lab1,1,0,1,1);
	        gri.add(lab2,2,0,1,1);
	        gri.add(lab3, 1,1 );
	        gri.add(cb1, 2,1 );
	        gri.add(btn, 1, 3);
	        FlowPane p1=new FlowPane();
	        p1.getChildren().add(h1);
	        p1.getChildren().add(gri);
	        p1.getChildren().add(txt);
	   	 Scene scene=new Scene(p1,600,600);
	   	 primaryStage.setScene(scene);
	   	 primaryStage.show();

		 cb1.getSelectionModel().selectedIndexProperty().addListener(new ChangeListener() {
				@Override
				public void changed(ObservableValue observable, Object oldValue, Object newValue) {
			
					btn.setOnAction(new EventHandler<ActionEvent>() {
						@Override
						public void handle(ActionEvent event) {
							PolicyProducer pp=null;
							if((newValue.toString()).compareTo("0")==0) {pp=new BodyPolicy();}
							else if((newValue.toString()).compareTo("1")==0){pp=new CarPolicy();}
					        else if((newValue.toString()).compareTo("2")==0) {pp=new PersonPolicy();}
							AutoInsurance ai=pp.getInsurObj();
							txt.setText(ai.getInfo());		
							
							
						}
						
					});
			
				}
			 });
			}
		}
